<?php

namespace App\Http\Controllers\Admin\Config;

use App\Http\Requests\Admin\Config\ChannelRequest;
use App\Models\Config\Channels;
use App\Models\Enum\Status;
use App\Services\Config\ChannelsService;

use App\Http\Controllers\Admin\BaseController;
use App\Services\Config\GamesService;
use Illuminate\Http\Request;

class ChannelsController extends BaseController
{

    public function __construct(GamesService $gamesService, ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
        $this->gamesService = $gamesService;
    }

    public function index(Request $request)
    {
        $reqGameId = $request->input('game_id', '');
        $reqPageSize = $request->page_size;
        $channels = $this->channelsService->getPage($request);

        return view('config.channels.index', compact('channels', 'reqGameId', 'reqPageSize'));
    }

    public function create()
    {
        $games = $this->gamesService->getGames(['status' => Status::NORMAL]);

        return view('config.channels.create', compact('games'));
    }

    public function store(ChannelRequest $request)
    {
        Channels::create($request->all());

        flash('添加成功')->success()->important();

        return $this->redirect($request);
    }

    public function edit($id)
    {
        $channel = $this->channelsService->ById($id);
        $games = $this->gamesService->getGames(['status' => Status::NORMAL]);
        $reqGameId = $channel->game_id;

        return view('config.channels.edit', compact('channel', 'games', 'reqGameId'));
    }

    public function update(ChannelRequest $request, $id)
    {
        $this->channelsService->update($request, $id);

        flash('更新游戏成功')->success()->important();

        return $this->redirect($request);
    }

    public function delete($id)
    {
        $channel = $this->channelsService->ById($id);

        if (empty($channel)) {
            flash('删除失败')->error()->important();

            return redirect()->back();
        }

        $channel->delete();

        flash('删除成功')->success()->important();

        return redirect()->back();
    }

    public function status($status, $id)
    {
        $game = $this->channelsService->ById($id);

        if (empty($game)) {
            flash('操作失败')->error()->important();

            return redirect()->back();
        }

        $game->update(['status' => $status]);

        flash('更新状态成功')->success()->important();

        return redirect()->back();
    }

}
